package com.rc.saas.mini.service.ugs;

import com.rc.saas.mini.mapper.ugs.TenantUgsSuperLevelPriceMapper;
import com.rc.saas.mini.mapper.ugs.UgsMemberSuperLevelMapper;
import com.rc.saas.mini.model.ugs.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class UgsMemberSuperLevelServiceImpl implements UgsMemberSuperLevelService {

    @Autowired
    private UgsMemberSuperLevelMapper ugsMemberSuperLevelMapper;

    @Autowired
    private TenantUgsSuperLevelPriceMapper tenantUgsSuperLevelPriceMapper;

    @Override
    public UgsMemberSuperLevelMapper getMapper() {
        return ugsMemberSuperLevelMapper;
    }

    @Override
    public void updateBySuperLevelOrder(UgsMemberSuperLevelOrder ugsMemberSuperLevelOrder) {
        if ("0".equals(ugsMemberSuperLevelOrder.getStatus())) {
            UgsMemberSuperLevelExample ugsMemberSuperLevelExample = new UgsMemberSuperLevelExample();
            ugsMemberSuperLevelExample.createCriteria().andTenantCodeEqualTo(ugsMemberSuperLevelOrder.getTenantCode()).andMemberIdEqualTo(ugsMemberSuperLevelOrder.getMemberId());
            List<UgsMemberSuperLevel> ugsMemberSuperLevels = ugsMemberSuperLevelMapper.selectByExample(ugsMemberSuperLevelExample);
            //tenant 会员的参数 tenant_ugs_super_level_price
            TenantUgsSuperLevelPriceExample example = new TenantUgsSuperLevelPriceExample();
            example.createCriteria().andTenantCodeEqualTo(ugsMemberSuperLevelOrder.getTenantCode()).andStatusEqualTo("0").andSuperLevelTypeEqualTo(ugsMemberSuperLevelOrder.getSuperLevelType());
            List <TenantUgsSuperLevelPrice> tenantUgsSuperLevelPrices = tenantUgsSuperLevelPriceMapper.selectByExample(example);
            if (tenantUgsSuperLevelPrices != null && tenantUgsSuperLevelPrices.size() > 0) {
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(new Date());
                if (ugsMemberSuperLevels != null && ugsMemberSuperLevels.size() > 0) {
                    ugsMemberSuperLevels.get(0).setRecordType("0");
                    ugsMemberSuperLevels.get(0).setTotalAmount(ugsMemberSuperLevels.get(0).getTotalAmount().add(ugsMemberSuperLevelOrder.getFactAmount()));
                    ugsMemberSuperLevels.get(0).setStatus("0");
                    ugsMemberSuperLevels.get(0).setSuperLevelType(ugsMemberSuperLevelOrder.getSuperLevelType());
                    //todo 添加时间
                    if (ugsMemberSuperLevels.get(0).getEndTime().compareTo(new Date()) == 1) {
                        calendar.setTime(ugsMemberSuperLevels.get(0).getEndTime());
                    }else {
                        ugsMemberSuperLevels.get(0).setBeginTime(new Date());
                    }
                    calendar.add(Calendar.DATE, tenantUgsSuperLevelPrices.get(0).getCardExpireDay());
                    ugsMemberSuperLevels.get(0).setEndTime(calendar.getTime());
                    ugsMemberSuperLevels.get(0).setLastUpdatedTime(new Date());
                    ugsMemberSuperLevelMapper.updateByPrimaryKeySelective(ugsMemberSuperLevels.get(0));
                } else {
                    //没有记录
                    UgsMemberSuperLevel ugsMemberSuperLevel = new UgsMemberSuperLevel();
                    ugsMemberSuperLevel.setTenantCode(ugsMemberSuperLevelOrder.getTenantCode());
                    ugsMemberSuperLevel.setMemberId(ugsMemberSuperLevelOrder.getMemberId());
                    ugsMemberSuperLevel.setRecordType("0");//达成方式 0购买 1赠送 默认0
                    ugsMemberSuperLevel.setSuperLevelType(ugsMemberSuperLevelOrder.getSuperLevelType());
                    ugsMemberSuperLevel.setTotalAmount(ugsMemberSuperLevelOrder.getFactAmount());
                    ugsMemberSuperLevel.setBeginTime(new Date());
                    calendar.add(Calendar.DATE, tenantUgsSuperLevelPrices.get(0).getCardExpireDay());
                    ugsMemberSuperLevel.setEndTime(calendar.getTime());
                    ugsMemberSuperLevel.setStatus("0");
                    ugsMemberSuperLevel.setCreatedTime(new Date());
                    ugsMemberSuperLevel.setLastUpdatedTime(new Date());
                    ugsMemberSuperLevelMapper.insertSelective(ugsMemberSuperLevel);
                }
            }
        }


    }
}
